$(function(){
    $("#list").mothinDatagrid({
        url:'system/dictionary!queryTypes.action',
        sortName: 'name',
        sortOrder: 'asc',
        columns:[[
            {field:'name',title:'名称',width:300,searchcolumn:"string",fixed:true,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'innate',title:'是否系统定义项',width:100,fixed:true,align:'center',resizable:false,formatter:function(value){
                if(value) {
                    return "√";
                }
                return "";
            }},
            {field:'treeStructure',title:'多级结构',width:100,fixed:true,align:'center',resizable:false,formatter:function(value){
                if(value) {
                    return "√";
                }
                return "";
            }},
	        {field:'memo',title:'备注',width:400,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'id',title:'操作',width:0,fixed:true,resizable:false,formatter:function(value,row){
                var data=frame_queryOperation(History.getState().url);
                if(data&&data.rows&&data.rows.length>0){
                    var link="";
                    for(var i=0;i<data.rows.length;i++){
                        if(row.innate) {
                            if(data.rows[i].action == "system/dictionary!deleteType.action") {
                                break;
                            }
                        }
                        link+='<button class="btn btn-link btn-xs" onclick="handle(\''+data.rows[i].id+'\',\''+data.rows[i].action+'\',\''+data.rows[i].name+'\',\''+value+'\');"><span class="'+data.rows[i].ico+'" />&nbsp;'+data.rows[i].name+'</button>';
                    }
                    return link;
                }
            }}
        ]]
    },handle);
});

function handle(id,action,name,bizId) {
    switch(action){
    case 'system/dictionary!addType.action':
        showDialog(action,'添加新字典类型',800,400,function(){
            $("#list").datagrid("reload");
        });
        break;
     case 'system/dictionary!updateType.action':
         showDialog(action+"?dictionaryType.id="+bizId+"&__t="+$.mothin.util.getStamp(),'修改新字典类型',800,400,function(){
             $("#list").datagrid("reload");
         });
         break;
     case 'system/dictionary!config.action':
         showDialog(action+"?dictionaryType.id="+bizId+"&__t="+$.mothin.util.getStamp(),'配置字典项',1000,500,function(){
             $("#list").datagrid("reload");
         });
         break;
     case 'system/dictionary!deleteType.action':
         showConfirm('确认删除此字典类型吗？',function(){
             $.ajax({
                 type: 'POST',
                 url: action,
                 data: {"dictionaryType.id":bizId},
                 success: function(data){
                     if(data.ok){
                         showMessage('字典类型删除成功',function(){
                             $("#list").datagrid("reload");
                         });
                     }else{
                         showMessage('字典类型删除失败：'+data.message,null,'error');
                     }
                 }
             });
         });
         break;
     }	
}